#include <gpio.h>
#include <thumips.h>

static
void led_rg_write(int num, int value)
{
  if (num % 2 == 0) {
    outw(LED_RG0_ADDR, value);
  } else {
    outw(LED_RG1_ADDR, value);
  }
}

void led_rg_blank(int num)
{
  led_rg_write(num, LED_RG_BLANK);
}

void led_rg_green(int num)
{
  led_rg_write(num, LED_RG_GREEN);
}

void led_rg_red(int num)
{
  led_rg_write(num, LED_RG_RED);
}

void led_line(int num)
{
  outw(LED_LINE_ADDR, num);
}

void num_display(int value)
{
  outw(NUM_ADDR, value);
}

int num_read(void)
{
  return inw(NUM_ADDR);
}

int get_user_press() {
  volatile int pb;
  int pb_tmp;
  int pb_0;
  int pb_1;
  int pb_2;
  int pb_3;

  do {
    pb = (inw(BTN_KEY_ADDR) & 0x0000f000)>>12;
  } while (pb == 0);
  
  pb_tmp = pb;
  pb_0 = (pb_tmp >>3)&1;
  pb_1 = ((pb_tmp >>2)&1)<<1;
  pb_2 = ((pb_tmp >>1)&1)<<2;
  pb_3 = ((pb_tmp)&1)<<3;
  pb_tmp = pb_0 | pb_1 | pb_2 | pb_3;
  return pb_tmp;
}

void get_user_release() {
  volatile int pb;

  do {
    pb = (inw(BTN_KEY_ADDR) & 0xf000)>>12;
  } while (pb != 0);
}
